package cn.iocoder.yudao.module.pickbook.service.series;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pickbook.controller.admin.series.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.series.SeriesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.series.SeriesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 绘本系列 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SeriesServiceImpl implements SeriesService {

    @Resource
    private SeriesMapper seriesMapper;

    @Override
    public Long createSeries(SeriesSaveReqVO createReqVO) {
        // 检查绘本系列是否已存在，这里以 title 为例
        String title = createReqVO.getTitle();
        SeriesDO existingSeries = seriesMapper.selectByTitle(title);

        // 插入
        SeriesDO series = BeanUtils.toBean(createReqVO, SeriesDO.class);

        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        series.setId(id);

        seriesMapper.insert(series);

        // 返回
        return series.getId();
    }



    @Override
    public void updateSeries(SeriesSaveReqVO updateReqVO) {
        // 校验存在
        validateSeriesExists(updateReqVO.getId());
        // 更新
        SeriesDO updateObj = BeanUtils.toBean(updateReqVO, SeriesDO.class);
        seriesMapper.updateById(updateObj);
    }

    @Override
    public void deleteSeries(Long id) {
        // 校验存在
        validateSeriesExists(id);
        // 删除
        seriesMapper.deleteById(id);
    }

    @Override
        public void deleteSeriesListByIds(List<Long> ids) {
        // 删除
        seriesMapper.deleteByIds(ids);
        }


    private void validateSeriesExists(Long id) {
        if (seriesMapper.selectById(id) == null) {
            throw exception(SERIES_NOT_EXISTS);
        }
    }

    @Override
    public SeriesDO getSeries(Long id) {
        return seriesMapper.selectById(id);
    }

    @Override
    public PageResult<SeriesDO> getSeriesPage(SeriesPageReqVO pageReqVO) {
        return seriesMapper.selectPage(pageReqVO);
    }

}